jQuery(function($) {
	var doc = $(document),
		doc_ = document;
	
	function setBackgroundSize() {
		$(".banner").addClass("zoom");
	}
	setBackgroundSize();
	
	
	
	/**图片轮播**/
	
	if (M === undefined) {
		var M = {}
	}
	M.create = function(elm) {
		return doc_.createElement(elm);
	};
	
	M.Slide = function() {
		var that = this;
		that.slideBox = doc_.querySelector(".slide");
		that.gallery = doc_.querySelectorAll(".gallery li"),
		that.nav = doc_.querySelector(".gallery_nav");
		that.lis = null;
		that.liWidth = that.gallery[0].offsetWidth;
		that.len = that.gallery.length;
		that.index = 0;
		that.timer = 0;
	};
	
	slide = M.slide = new M.Slide();
	
	slide.setIndex = function(lis) {

		for (var i = 0, n = lis.length; i < n; i++) {
			lis[i].index = i;
		}
		lis[0].className = "active";
	};
	
	slide.createElm = function() {
		var that = this,
			i = 0,
			item = that.gallery,
			len = item.length,
			li = "",
			nav = that.nav,
			fragment = doc_.createDocumentFragment();
			
		for (i; i < len; i++) {
			li = M.create("li");
			fragment.appendChild(li);
		}
		
		nav.appendChild(fragment);
		that.navLi = that.nav.querySelectorAll("li");
		slide.setIndex(that.navLi);
	};
	slide.createElm();
	
	slide.moveElement = function(index, num) {
		var that = this,
			box = doc_.querySelector(".gallery"),
			lis = doc_.querySelectorAll(".gallery_nav li");
			
		that.lis = lis;
		
		for (var i = 0, n = lis.length; i < n; i++) {
			lis[i].className = "";
		}
		
		lis[num].className = "active";
		box.style.left = index + "px";
	};
	
	slide.onMouseOver = function(event) {
		var target = event.target,
			index = target.index;
		if (target.tagName === "LI") {
			var lis = doc_.querySelectorAll(".gallery_nav li");
			lis[index].className = "active";
			slide.moveElement(-(slide.liWidth * index), index);
		}
	};

	slide.autoPlay = function() {
		clearInterval(slide.timer);
		slide.timer = setInterval(function() {
			slide.index++;
			
			if (slide.index > slide.len - 1) {
				slide.index = 0;
			}
			
			slide.moveElement(-(slide.index * slide.liWidth), slide.index);
		}, 3000);
		
	};
	slide.autoPlay();

	slide.nav.addEventListener("mouseover", function() {
		slide.onMouseOver(event);
	}, false);
	
	slide.slideBox.addEventListener("mouseover", function() {
		clearInterval(slide.timer);
	}, false);
	
	slide.slideBox.addEventListener("mouseout", function() {
		slide.autoPlay();
	}, false);
		
	slide.slideBox.addEventListener("click", function(event) {
		var target = event.target,
			classname = target.className;
		
		if (target.tagName === "EM") {
			switch(classname) {
				case "next_":
					slide.index++;
					if (slide.index > slide.len - 1) {
						slide.index = 0;
					}
					slide.moveElement(-(slide.index * slide.liWidth), slide.index);
					break;
				case "pre":
					if (slide.index <= 0) {
						slide.index = slide.len;
					}
					slide.index--;
					slide.moveElement(-(slide.index * slide.liWidth), slide.index);
					break;
			}
		}
	}, false);
});